﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SQLCompact;
using BrightIdeasSoftware;
using PictureOrganizer3.Properties;
using log4net;

namespace PictureOrganizer3
{
    public partial class MainForm : Form
    {
        private ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private Database ctx = new Database(@"Data source = Database\Database.sdf");
        private List<Comic> comics;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            log4net.Config.XmlConfigurator.Configure();
            this.Text = String.Format(this.Text, this.GetType().Assembly.GetName().Version);
            olvColumnIsRead.Renderer = new MappedImageRenderer(true, Resources.accept, false, Resources.delete);
            olvColumnRating.AspectGetter = r => String.Format("star_{0:00}", ((Comic)r).Rating);
            olvColumnRating.Renderer = new MappedImageRenderer(new Object[] {
            "star_01", Resources.star_01, 
            "star_02", Resources.star_02, 
            "star_03", Resources.star_03, 
            "star_04", Resources.star_04, 
            "star_05", Resources.star_05, 
            "star_06", Resources.star_06, 
            "star_07", Resources.star_07, 
            "star_08", Resources.star_08, 
            "star_09", Resources.star_09, 
            "star_10", Resources.star_10 });

            // Ersten 50 Einträge laden
            comics = (from c in ctx.Comics select c).Take(50).ToList();

            dataListView.DataSource = comics;
        }

        private void tsbtn_OpenDirectory_Click(object sender, EventArgs e)
        {
#if DEBUG
            folderBrowserDialog.SelectedPath = @"C:\Download\test";
            logger.DebugFormat("Pfadvorbelegung auf {0}", folderBrowserDialog.SelectedPath);
#endif

            if (folderBrowserDialog.ShowDialog(this) == DialogResult.OK)
            {
                logger.InfoFormat("Der Ordner {0} wurde ausgewählt", folderBrowserDialog.SelectedPath);
                using (var dlg = new ScanForm(folderBrowserDialog.SelectedPath, ctx))
                {
                    dlg.ShowDialog(this);
                    // Ersten 50 Einträge laden
                    comics = (from c in ctx.Comics select c).Take(50).ToList();
                }
            }
        }

        private void dataListView_DoubleClick(object sender, EventArgs e)
        {

            using (var dlg = new ComicForm((Comic)dataListView.SelectedObject, ctx))
            {
                dlg.ShowDialog(this);
                comics = (from c in ctx.Comics select c).Take(50).ToList();
            }
        }

        private void tsb_About_Click(object sender, EventArgs e)
        {
            using (var dlg = new AboutForm())
            {
                dlg.ShowDialog(this);
            }
        }

        private void tsbtn_ManageAuthors_Click(object sender, EventArgs e)
        {
            using (var dlg = new ManageAuthorsForm(ctx))
            {
                dlg.ShowDialog(this);
            }
        }

        #region Gruppierung

        private void SetGroupByCheckState(ToolStripMenuItem currentItem)
        {
            // Zurerst alle abhacken
            tsmi_GroupByAuthor.Checked = false;
            tsmi_GroupByLanguage.Checked = false;
            tsmi_GroupByDirectory.Checked = false;
            tsmi_NoGroup.Checked = false;

            // und dann den aktuellen anhacken.
            currentItem.Checked = true;
        }

        private void tsmi_GroupByAuthor_Click(object sender, EventArgs e)
        {
            SetGroupByCheckState((ToolStripMenuItem)sender);
            dataListView.BeginUpdate();
            dataListView.ShowGroups = true;
            olvColumnName.GroupKeyGetter = c => ((Comic)c).Author != null ? ((Comic)c).Author.Name : "Unbekannter Autor";
            dataListView.BuildGroups();
            dataListView.EndUpdate();
        }

        private void tsmi_GroupByLanguage_Click(object sender, EventArgs e)
        {
            SetGroupByCheckState((ToolStripMenuItem)sender);
            dataListView.BeginUpdate();
            dataListView.ShowGroups = true;
            olvColumnName.GroupKeyGetter = c => ((Comic)c).Language ?? "Unbekannte Sprache";
            dataListView.BuildGroups();
            dataListView.EndUpdate();
        }

        private void tsmi_GroupByDirectory_Click(object sender, EventArgs e)
        {
            SetGroupByCheckState((ToolStripMenuItem)sender);
            dataListView.BeginUpdate();
            dataListView.ShowGroups = true;
            olvColumnName.GroupKeyGetter = c => ((Comic)c).DirectoryPath;
            dataListView.BuildGroups();
            dataListView.EndUpdate();
        }

        private void tsmi_NoGroup_Click(object sender, EventArgs e)
        {
            SetGroupByCheckState((ToolStripMenuItem)sender);
            dataListView.BeginUpdate();
            dataListView.ShowGroups = false;
            olvColumnName.GroupKeyGetter = null;
            dataListView.BuildGroups();
            dataListView.EndUpdate();
        }

        #endregion




    }
}
